{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c5fba256",
   "metadata": {},
   "source": [
    "# Fusion 360 Gallery Segmentation Dataset Viewer\n",
    "The easiest way to view segmentation data from the Fusion 360 Gallery segmentation dataset is by visualizing the mesh data and associated segmentation.   This can easily be done using python based mesh visualization tools.   Here we give a simple example which uses the [meshplot]( https://github.com/skoch9/meshplot) library internally."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a05de486",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pathlib import Path\n",
    "from segmentation_viewer import SegmentationViewer"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "635ad0c0",
   "metadata": {},
   "source": [
    "You will want to point the segmentation viewer at the `meshes` folder in the Fusion 360 Gallery segmentation dataset."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "db96d88b",
   "metadata": {},
   "outputs": [],
   "source": [
    "mesh_folder = Path(\"../testdata/segmentation_examples\")\n",
    "# You may want to sawp this for you download of the segmentation dataset \n",
    "# mesh_folder = Path(\"/data_drive/SegmentationDataset/s2.0.0/meshes\")\n",
    "viewer = SegmentationViewer(mesh_folder)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "81abe5c1",
   "metadata": {},
   "source": [
    "You can then find the filenames of the files in the dataset.  Here we search for the .`obj` files and extract the file stems.  The viewer requires just the file stem as it needs to locate both the `.obj` file and the corresponding `.seg` segmentation file."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "1cbe27dc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Number of files in the dataset 1\n"
     ]
    }
   ],
   "source": [
    "dataset_file_stems = [ f.stem for f in mesh_folder.glob(\"**/*.obj\")]\n",
    "print(f\"Number of files in the dataset {len(dataset_file_stems)}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6054a5fc",
   "metadata": {},
   "source": [
    "You can then choose which file to view based on it's index in the `dataset_file_stems`.  Alternatively if you want to view a specific file then you can simply use the filestem in the `view_segmentation()` function."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "864d5004",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Viewing file 102673_56775b8e_5\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "9f57c01976634fe09b01a48b5e4d6e32",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Renderer(camera=PerspectiveCamera(children=(DirectionalLight(color='white', intensity=0.6, position=(2.9802322…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "file_to_view = 0\n",
    "file_stem = dataset_file_stems[file_to_view]\n",
    "print(f\"Viewing file {file_stem}\")\n",
    "viewer.view_segmentation(file_stem)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
